﻿
namespace HSS.Collections
{
	#region Using Directives
	using System;
	using System.Collections;
	using System.Globalization;
	#endregion

	#region Comparer
	/// <summary>
	/// A default Comparer implementation.
	/// </summary>
	public sealed class Comparer : IComparer
	{
		#region Fields
		/// <summary>
		/// The default Comparer using the CurrentCulture.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
		public static readonly Comparer Default = new Comparer(CultureInfo.CurrentCulture);
		/// <summary>
		/// The default Comparer using the InvariantCulture.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
		public static readonly Comparer DefaultInvariant = new Comparer(CultureInfo.InvariantCulture);
		private CompareInfo m_compareInfo;
		private const string CompareInfoName = "CompareInfo";
		#endregion

		#region Constructors
		private Comparer()
		{
			this.m_compareInfo = null;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="culture">The CultureInfo used for the comparison</param>
		public Comparer(CultureInfo culture)
		{
			if (culture == null)
				throw new ArgumentNullException("culture");
			this.m_compareInfo = culture.CompareInfo;
		}
		#endregion

		#region Methods
		/// <summary>
		/// Compare to objects
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		public int Compare(object x, object y)
		{
			if (x == y)
				return 0;
			if (x == null)
				return -1;
			if (y == null)
				return 1;
			if (this.m_compareInfo != null)
			{
				string str = x as string;
				string str2 = y as string;
				if ((str != null) && (str2 != null))
					return this.m_compareInfo.Compare(str, str2);
			}
			IComparable comparable = x as IComparable;
			if (comparable == null)
				throw new ArgumentException("Argument does not implement IComparable");
			return comparable.CompareTo(y);
		}
		#endregion
	}
	#endregion
}
